#!/bin/sh /etc/rc.common
# Copyright (C) 2015 OpenWrt.org

START=80

USE_PROCD=1

NGINX_UTIL="/usr/bin/nginx-util"

eval $("${NGINX_UTIL}" get_env)

start_service() {
	[ -d /var/log/nginx ] || mkdir -p /var/log/nginx
	[ -d /var/lib/nginx ] || mkdir -p /var/lib/nginx

	${NGINX_UTIL} init_lan

	procd_open_instance
	NCPUS="$(grep -c '^processor\s*:' /proc/cpuinfo)"
	procd_set_param command /usr/sbin/nginx -c "${NGINX_CONF}" \
		-g "daemon off; worker_processes $NCPUS;"
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_set_param file "${LAN_LISTEN}" "${LAN_LISTEN}.default" \
		"${NGINX_CONF}" "${CONF_DIR}*.conf" "${CONF_DIR}*.locations"
	[ "${LAN_SSL_LISTEN}" == "" ] \
	|| procd_append_param file "${CONF_DIR}*.crt" "${CONF_DIR}*.key" \
		"${LAN_SSL_LISTEN}" "${LAN_SSL_LISTEN}.default"
	procd_set_param respawn
	procd_close_instance
}

stop_service() {
	rm -f "${LAN_LISTEN}" "${LAN_LISTEN}.default"
	[ "${LAN_SSL_LISTEN}" == "" ] \
	|| rm -f "${LAN_SSL_LISTEN}" "${LAN_SSL_LISTEN}.default"
}

service_triggers() {
	procd_add_reload_interface_trigger loopback
	procd_add_reload_interface_trigger lan
}

reload_service() {
	[ -d /var/log/nginx ] || mkdir -p /var/log/nginx
	[ -d /var/lib/nginx ] || mkdir -p /var/lib/nginx

	${NGINX_UTIL} init_lan

	procd_send_signal nginx
}

relog() {
	[ -d /var/log/nginx ] || mkdir -p /var/log/nginx
	procd_send_signal nginx '*' USR1
}

EXTRA_COMMANDS="relog"
EXTRA_HELP="	relog	Reopen log files (without reloading)"
